<?php
namespace app\admin\controller;

use think\facade\View;
use think\exception\ValidateException;
use app\admin\BaseController;
use app\admin\model\Order as OrderModel;
use app\admin\validate\Merchant as Validate;
/**
 * Merchant管理
 */
class Order extends BaseController{
    public function index()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("index")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            $input = input("post.");
            $count = OrderModel::withSearch(["keyword"], $input)->count();
            $data  = OrderModel::withSearch(["keyword"], $input)->with(['stop','merchant'])->order('id desc')->page($input["page"], $input["pageSize"])->select();
            //channel1微信2支付宝3兑换券
            $channelText=['/','微信','支付宝','兑换券'];
            //初始订单支付状态0待支付1支付成功2支付中3已关闭
            $payStatusText=['待支付','支付成功','支付中','已关闭'];
            $endTypeText=['/','/','补差价','退款'];
            $refundStatusText=['待退款','退款成功','退款中','退款失败'];
            $againPayStatusText=['待支付','支付成功','支付中','支付失败'];
            $signTypeText=['待进场','已进场','已离场','已完成'];
            if($data){
                foreach ($data as &$datum){
                    $datum['channel_text']=$channelText[$datum['channel']];
                    $datum['init_pay_status_text']=$payStatusText[$datum['init_pay_status']];
                    $datum['end_type_text']=$endTypeText[$datum['end_type']];
                    $datum['refund_status_text']=$datum['end_type']==3?$refundStatusText[$datum['refund_status']]:"/";
                    $datum['again_pay_status_text']=$datum['end_type']==2?$againPayStatusText[$datum['again_pay_status']]:'/';
                    $datum['sign_type_text']=$datum['init_pay_status']==1?$signTypeText[$datum['sign_type']]:'/';
                    $datum['stop_name']=$datum['stop']['stop_name'];
                    $datum['merchant_name']=$datum['merchant']['merchant_name'];
                    $datum['delete_disabled']=true;
                    $datum['update_disabled']=true;
                }
            }
            return json(["status" => "success", "message" => "请求成功", "data" => $data, "count" => $count]);
        } else {
            return View::fetch();
        }
    }
}